package com.zzh.wsss.controller;

import com.zzh.wsss.service.UserInterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
//解决跨域
@CrossOrigin//(origins = "http://10.133.69.253:8081",allowedHeaders = "*",methods = {},allowCredentials = "true")
public class UserInterfaceController {
    @Autowired
    private UserInterfaceService userInterfaceService;

    //上传头像
    @RequestMapping(value = "/pictureupload",method = RequestMethod.POST)
    public Map<String,Object> upHeadportrait(MultipartFile photo,String username ,HttpSession session) throws
            IOException {
        //获取上传的文件的文件名
        String filename = photo.getOriginalFilename();
        //处理文件重名问题
        String hzName = filename.substring(filename.lastIndexOf("."));


        filename = userInterfaceService.getuserid(username).getId() + hzName;
        //获取服务器中photo目录的路径
        ServletContext servletContext = session.getServletContext();
        String photoPath = servletContext.getRealPath("img");

        File file = new File(photoPath);
        if(!file.exists()){
            file.mkdir();
        }
        String finalPath = photoPath + File.separator + filename;
        //实现上传功能
        photo.transferTo(new File(finalPath));
        userInterfaceService.setuserheadportrait(username,filename);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("headportrait",filename);
        return map;
    }

    //更改昵称
    @RequestMapping(value = "/updatanick",method = RequestMethod.PUT)
    private Map<String,Object> updataNickname(@RequestBody Map<String,String> map){
        userInterfaceService.setusernickname(map.get("username"),map.get("nickname"));
        Map<String,Object> map1=new HashMap<String, Object>();
        map1.put("nickname",map.get("nickname"));
        return map1;
    }

    //更改密码
    @RequestMapping(value = "/updatapassword",method = RequestMethod.PUT)
    private Map<String,Object> updataPassword(@RequestBody Map<String,String> map){
        Boolean success=userInterfaceService.updatapassword(map.get("username"),map.get("oldpassword"),map.get("newpassword"));
        Map<String,Object> map1=new HashMap<String, Object>();
        map1.put("success",success);
        return map1;
    }

//    @RequestMapping("/testDown")  以字节流向前端输出图片
//    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws
//            IOException {
//        ServletContext servletContext = session.getServletContext();
//        String realPath = servletContext.getRealPath("/img/test1.jpg");
//        InputStream is = new FileInputStream(realPath);
//        byte[] bytes = new byte[is.available()];
//        is.read(bytes);
//        final HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.IMAGE_JPEG);
//        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes,headers, HttpStatus.OK);
//        is.close();
//        return responseEntity;
//    }


}
